﻿
using System;
using System.Text;

namespace Wechat.Results
{
    public interface IWechatResult
    {
        string ToString();
    }

    /// <summary>
    /// 微信返回结果
    /// </summary>
    public abstract class WechatResult : IWechatResult
    {
        public WechatResult(string toUsername, string fromUsername)
        {
            ToUserName = toUsername;
            FromUserName = fromUsername;
            CreateTime = DateTime.Now;
        }

        /// <summary>
        /// 接收方
        /// </summary>
        public string ToUserName { get; protected set; }

        /// <summary>
        /// 发送方
        /// </summary>
        public string FromUserName { get; protected set; }
        
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; protected set; }

        protected abstract void Format(StringBuilder builder);

        public override string ToString()
        {
            var sb = new StringBuilder();

            sb.Append("<xml>");
            
            sb.AppendFormat("<ToUserName><![CDATA[{0}]]></ToUserName>", ToUserName);
            
            sb.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", FromUserName);

            sb.AppendFormat("<CreateTime>{0}</CreateTime>", Utilities.ConvertDateTimeInt(CreateTime));

            Format(sb);

            sb.Append("</xml>");
            
            return sb.ToString();
        }
    }
}
